package com.cmc6.role.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc6.common.model.dto.NotEmptyIdSet;
import com.cmc6.common.model.dto.NotNullId;
import com.cmc6.common.model.entity.RoleDO;
import com.cmc6.common.util.ApiResult;
import com.cmc6.role.model.dto.RoleInsertOrUpdateDTO;
import com.cmc6.role.model.dto.RolePageDTO;
import com.cmc6.role.model.vo.RolePageVO;
import com.cmc6.role.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "角色控制器")
@RequestMapping("/role")
@RestController
public class RoleController {

    @Resource
    RoleService baseService;

    @ApiOperation(value = "新增/修改 角色")
    @PostMapping("/insertOrUpdate")
    @PreAuthorize("hasAuthority('role:insertOrUpdate')")
    public ApiResult<String> insertOrUpdate(@RequestBody @Valid RoleInsertOrUpdateDTO dto) {
        return ApiResult.ok(baseService.insertOrUpdate(dto));
    }

    @ApiOperation(value = "分页排序查询：角色")
    @PostMapping("/page")
    @PreAuthorize("hasAuthority('role:page')")
    public ApiResult<Page<RoleDO>> myPage(@RequestBody @Valid RolePageDTO dto) {
        return ApiResult.ok(baseService.myPage(dto));
    }

    @ApiOperation(value = "通过主键id，查看详情")
    @PostMapping("/infoById")
    @PreAuthorize("hasAuthority('role:infoById')")
    public ApiResult<RolePageVO> infoById(@RequestBody @Valid NotNullId notNullId) {
        return ApiResult.ok(baseService.infoById(notNullId));
    }

    @ApiOperation(value = "删除角色")
    @PostMapping("/deleteByIdSet")
    @PreAuthorize("hasAuthority('role:deleteByIdSet')")
    public ApiResult<String> deleteByIdSet(@RequestBody @Valid NotEmptyIdSet notEmptyIdSet) {
        return ApiResult.ok(baseService.deleteByIdSet(notEmptyIdSet));
    }

}
